package gridy.master;

import gridy.worker.stub.WorkerServiceStub;

import java.util.LinkedList;
import java.util.List;

import net.sf.saxon.expr.AxisExpression;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Scope;

@EagerInit
@Scope("COMPOSITE")
public class MasterImpl implements Master {
    
    private List<String> workers = new LinkedList<String>();
    
    public MasterImpl() {
        System.out.println();
        System.out.println("Tworze MasterImpl");
        System.out.println();
    }
    
    @Override
    public void register(String workerAddress) {
        
        System.out.println("Worker '"+workerAddress+"' wywołał register");
        
        for (String worker : workers) {
            System.out.println("\tObecnie byli następujący workerzy: "+worker);
        }
        System.out.println();
        
        workers.add(workerAddress);
        
        
        callback();
    }

    private void callback() {
        
        try {
            ServiceClient sc = new ServiceClient();
            // create option object
            Options opts = new Options();
            //setting target EPR
            opts.setTo(new EndpointReference("http://localhost:8085/WorkerService"));
            //Setting action ,and which can be found from the wsdl of the service
            //setting created option into service client
            sc.setOptions(opts);
            
            sc.fireAndForget(createPayLoad());
            //sc.fireAndForget(createHelloPayLoad());
            //WorkerServiceStub wss = new WorkerServiceStub("http://localhost:8084/WorkerService");
            //WorkerServiceStub.Calculate calc = new WorkerServiceStub.Calculate();
            //calc.setArg0(new double[] { 1, 2, 3, 0, 10 });
            //wss.calculate(calc);
            
            //sc.sendRobust(createPayLoad());
            //System.out.println(res);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public OMElement createPayLoad() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://ws.apache.org/axis2/xsd", "ns1");
        OMElement method = fac.createOMElement("calculate", omNs);
        
        OMElement value = fac.createOMElement("arg0", omNs);
        value.setText("1.0");
        method.addChild(value);
        
        value = fac.createOMElement("arg0", omNs);
        value.setText("3.0");
        method.addChild(value);
        
        value = fac.createOMElement("arg0", omNs);
        value.setText("2.0");
        method.addChild(value);
        
        return method;
    }
    public OMElement createHelloPayLoad() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://ws.apache.org/axis2/xsd", "ns1");
        OMElement method = fac.createOMElement("hello", omNs);
        OMElement value = fac.createOMElement("arg0", omNs);
        value.setText("WiadomosC");
        method.addChild(value);
        return method;
    }
    
}
